/**
 * Created by Satrong on 2014/8/3.
 * Knockoutjs 分页
 */

/**
 *   dom {[object HTMLElement]} 元素对象，如：document.getElementById("foo")
 *   url {String} 获取数据的地址，返回的数据格式：{total:'总页数(Number类型)',list:'数组(Array)'}
 *   viewmodel {Function} 构造函数，显示数据的属性名必须是list,通过 new viewmodel().list([]) 来显示数据列表
 */

var Page = function (dom, url, viewmodel) {
    var that = this;
    this.dom = dom;
    this.url = url;
    this.vm = viewmodel || function () { };
    this.PageHtml();
    this.GetData(function (data) {
        that.PageLink(data);
    });
}

Page.prototype.GetData = function (callback, p) {
    var data = {};
    data.pageSize = 20;
    data.pageIndex = 1;
    if (typeof p === "object") {
        $.extend(data, p);
    } else {
        data.pageIndex = p || 1;
    }
    $.ajax({
        url: this.url,
        cache: false,
        async: false,
        data: data,
		dataType: 'json',
        success: function (json) {
            callback(json);
        },
        error: function () {
            alert("获取数据失败");
        }
    });
}

Page.prototype.PageHtml = function () {
    var str = '<div class="ko-grid-pageLinks"><span class="page-inner" data-bind="foreach:pageList">'
		 + '<a href="#" data-bind="html:$data,click:$root.pageTurn,css:$data===$root.current()?\'current\':\'\'"></a>'
		 + '</span></div>';
    $(this.dom).append(str);
}

Page.prototype.PageLink = function (data) {
    var that = this;
    var vm = new this.vm();
    vm.list && vm.list(data.list);
    vm.pages = ko.observable(data.list.length === 0 ? 0 : Math.ceil(data.total / data.list.length));
    vm.current = ko.observable(1);
    vm.pageList = ko.observableArray(that.Range(vm.current(), vm.pages()));
    vm.options = {};
    vm.pageTurn = function ($data) {
        if (/^\d+$/.test($data) && ($data < 1 || $data > vm.pages()))
            return;
        if ($data === "&laquo;") {
            $data = vm.current() - 1;
        } else if ($data === "&raquo;") {
            $data = vm.current() + 1;
        }
        if ($data < 1 || $data > vm.pages())
            return;
        that.GetData(function (data) {
            vm.list && vm.list(data.list);
            vm.current($data);
            vm.pageList(that.Range($data, vm.pages()));
        }, $.extend(vm.options, { pageIndex: $data }));
    }
    vm.klass = function (type, current, pages) {
        if (/^\d+$/.test(type)) {
            return '';
        } else {
            if (current === 1 && type === "&laquo;") {
                return 'disabled';
            } else if (current === pages && type === "&raquo;") {
                return 'disabled';
            }
        }
    }
    this.ViewModel = vm;
    ko.applyBindings(vm, that.dom);
}

Page.prototype.Search = function (options) {
    var that = this;
    this.GetData(function (json) {
        that.ViewModel.list(json.list);
        that.ViewModel.options = options;
        that.ViewModel.current(1);
        that.ViewModel.pages(json.list.length === 0 ? 0 : Math.ceil(json.total / json.list.length));
        that.ViewModel.pageList(that.Range(1, that.ViewModel.pages()));
    }, options);
}

Page.prototype.Range = function (current, pages) {
    var start = 1;
    if (pages < 5) {
        start = 1;
    } else if (current < 3) {
        start = 1;
    } else if (current + 2 > pages) {
        start = pages - 4;
    } else {
        start = current - 2;
    }
    var arr = ko.utils.range(start, start + 4 > pages ? pages : start + 4);
    arr.unshift('&laquo;');
    arr.push('&raquo;');
    return arr.length < 4 ? [] : arr;
}
